﻿using System;
using System.Collections.Generic;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using SX.Core.Swagger.Filters;

namespace SX.Core.Swagger
{
    public static class ServiceCollectionExtensions
    {
        /// <summary>
        /// 添加Swagger
        /// </summary>
        /// <param name="services"></param>
        /// <param name="modules">模块集合</param>
        /// <returns></returns>
        public static IServiceCollection AddSwagger(this IServiceCollection services,string name,string title="",string version="V1")
        {
            if (string.IsNullOrWhiteSpace(title)) title = name;
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(name, new OpenApiInfo
                {
                    Title = title,
                    Version =version
                });
                var securityScheme = new OpenApiSecurityScheme
                {
                    Description = "认证请求头格式: \"Authorization: Bearer {token}\"",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                };

                //添加设置Token的按钮
                c.AddSecurityDefinition("Bearer", securityScheme);

                //添加Jwt验证设置
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            },
                            Scheme = "oauth2",
                            Name = "Bearer",
                            In = ParameterLocation.Header,
                        },
                        new List<string>()
                    }
                });

                //链接转小写过滤器
                c.DocumentFilter<LowercaseDocumentFilter>();

                //描述信息处理
                c.DocumentFilter<DescriptionDocumentFilter>();

                //隐藏属性
                c.SchemaFilter<IgnorePropertySchemaFilter>();
            });

            services.AddSwaggerGenNewtonsoftSupport();
            return services;
        }


        /// <summary>
        /// 添加Swagger 采用IdentityServer4验证方式
        /// </summary>
        /// <param name="services"></param>
        /// <param name="authorityUrl">id4地址</param>
        /// <param name="apiName">API name</param>
        /// <param name="title">标题</param>
        /// <param name="version">版本</param>
        /// <returns></returns>
        public static IServiceCollection AddSwaggerId4(this IServiceCollection services,string authorityUrl , string apiName, string title = "", string version = "V1")
        {
            if (string.IsNullOrWhiteSpace(title)) title = apiName;

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc(apiName, new OpenApiInfo
                {
                    Title = title,
                    Version = version
                });

                //添加Jwt验证按钮
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                        {
                            {
                                new OpenApiSecurityScheme
                                {
                                    Reference = new OpenApiReference
                                    {
                                        Id = "oauth2",
                                        Type = ReferenceType.SecurityScheme
                                    }
                                },
                                new List<string>()
                            }
                        });

                //添加验证设置
                c.AddSecurityDefinition("oauth2", new OpenApiSecurityScheme
                {
                    Type = SecuritySchemeType.OAuth2,
                    Description = "oauth2登录授权",
                    Flows = new OpenApiOAuthFlows
                    {
                        Implicit = new OpenApiOAuthFlow
                        {
                            AuthorizationUrl = new Uri($"{authorityUrl}/connect/authorize"),
                            Scopes = new Dictionary<string, string>
                                    {
                                        { apiName, $"{apiName}接口API" }
                                    }
                        }
                    }
                });

                //链接转小写过滤器
                c.DocumentFilter<LowercaseDocumentFilter>();

                //描述信息处理
                c.DocumentFilter<DescriptionDocumentFilter>();

                //隐藏属性
                c.SchemaFilter<IgnorePropertySchemaFilter>();
            });

            services.AddSwaggerGenNewtonsoftSupport();

            return services;
        }
    }
}
